﻿<%@ Page Title="Thêm đơn hàng" Language="C#" MasterPageFile="~/OneColumn.Master"
	AutoEventWireup="true" CodeBehind="OrderEdit.aspx.cs" Inherits="KCL.Management.OrderEdit" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
	Chi tiết đơn hàng
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageHeader" runat="server">
	<link type="text/css" href="../styles/themes/base/jquery.ui.all.css" rel="stylesheet" />
	<link type="text/css" href="../styles/css/jquery-ui-numeric.css" rel="stylesheet" />
	<style type="text/css">
		.ui-autocomplete-input
		{
			margin: 7px 0 0 0;
			padding: 0 0 0.47em 0.45em;
		}
		.Dan div
		{
			font-size: 10pt;
			display: table;
		}
		.Dan span
		{
			color: CadetBlue;
		}
		.div-row
		{
			display: table-row;
			padding-bottom: 6px;
		}
		.div-cell
		{
			display: table-cell;
			width: 200px;
		}
		.icon-save
		{
			margin-left: 270px;
			font-size: larger !importtant;
		}
	</style>
	<script type="text/javascript" src="../styles/ui/jquery.ui.core.js"></script>
	<script type="text/javascript" src="../styles/ui/jquery.ui.widget.js"></script>
	<script type="text/javascript" src="../styles/ui/jquery.ui.position.js"></script>
	<script type="text/javascript" src="../styles/ui/jquery.ui.mouse.js"></script>
	<script type="text/javascript" src="../styles/ui/jquery.ui.draggable.js"></script>
	<script type="text/javascript" src="../styles/ui/jquery.ui.autocomplete.js"></script>
	<script type="text/javascript" src="../styles/ui/jquery.ui.dialog.js"></script>
	<script type="text/javascript" src="../styles/ui/jquery.ui.button.js"></script>
	<script type="text/javascript" src="../styles/ui/jquery.ui.datepicker.js"></script>
	<script type="text/javascript" src="../styles/ui/i18n/jquery.ui.datepicker-vi.js"></script>
	<script type="text/javascript" src="../styles/external/jquery.bgiframe-2.1.2.js"></script>
	<script type="text/javascript" src="../styles/js/jquery-ui-numeric-min.js"></script>
	<script type="text/javascript">
		var JSON = JSON || {};
		// implement JSON.stringify serialization
		JSON.stringify = JSON.stringify || function (obj) {
			var t = typeof (obj);
			if (t != "object" || obj === null) {
				// simple data type
				if (t == "string") obj = '"'+obj+'"';
				return String(obj);
			}
			else {
				// recurse array or object
				var n, v, json = [], arr = (obj && obj.constructor == Array);
				for (n in obj) {
					v = obj[n]; t = typeof(v);
					if (t == "string") v = '"'+v+'"';
					else if (t == "object" && v !== null) v = JSON.stringify(v);
					json.push((arr ? "" : '"' + n + '":') + String(v));
				}
				return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
			}
		};
		var numberOfMaterial = 0;
		var totalPrice = 0;
		<%= phaseList %>
        var currentOrder = <%= strCurrentOrder %>
		$(document).ready(function () {
			$("#deliveryDate").datepicker($.datepicker.regional['vi']);
			for (var i=1; i <= currentOrder.orderMaterials.length; i++) 
			{
			    AddMaterial();
			    $("#comboMaterials" + i + " option[value=" + currentOrder.orderMaterials[i-1].materialId + "]").attr("selected", true);
			    $("#description" + i).val(currentOrder.orderMaterials[i-1].description);
			    for (var currentPhase in currentOrder.orderMaterials[i-1].orderMaterialPhases)
			    {
			        $("#phaseWeight" + i + currentOrder.orderMaterials[i-1].orderMaterialPhases[currentPhase].phaseId).val(currentOrder.orderMaterials[i-1].orderMaterialPhases[currentPhase].weight);
			        $("#phaseQuantity" + i + currentOrder.orderMaterials[i-1].orderMaterialPhases[currentPhase].phaseId).val(currentOrder.orderMaterials[i-1].orderMaterialPhases[currentPhase].quantity);
			    }
			    RefreshPrice(i);
			    $("#quantity" + i).val(currentOrder.orderMaterials[i-1].orderQuantity);
			}
			RefreshMoney(1);
		});
		function SubmitOrder() {
			if ($("#customerId").val() == -1)
			{
				alert("Hãy chọn khách hàng");
				return false;
			}
			if (numberOfMaterial == 0)
			{
				alert("Hãy thêm nguyên liệu cho đơn hàng");
				return false;
			}
			var order = new Object();
			order.customerId = $("#customerId").val();
			order.strTotalPrice = $("#strTotalPrice").val();
			order.totalPrice = totalPrice;
			order.discount = $("#discount").val();
			order.prePay = $("#prePay").val();
			order.remain = $("#remain").val();
			order.deliveryDate = $("#deliveryDate").val();
			order.packageId = $("[name=packageId]:checked").val();
			order.createdBy = <%= Session["userId"] %>;
			order.orderMaterials = new Array();
			for (var i=1; i <= numberOfMaterial; i++)
			{
				var orderMaterial = new Object();
				orderMaterial.materialId = $("#comboMaterials" + i + " option:selected").val();
				orderMaterial.description = $("#description" + i).val();
				orderMaterial.orderQuantity = $("#quantity" + i).val();
				orderMaterial.orderMaterialPhases = new Array();
				for (var j in phases)
					if ($("#phaseQuantity" + i + phases[j].id).val() != "")
					{
						var orderMaterialPhase = new Object();
						orderMaterialPhase.phaseId = phases[j].id;
						if ($("#phaseWeight" + i + phases[j].id).val() != "" && !isNaN(parseFloat($("#phaseWeight" + i + phases[j].id).val())))
							orderMaterialPhase.weight = parseFloat($("#phaseWeight" + i + phases[j].id).val());
						else
							orderMaterialPhase.weight = 1;
						orderMaterialPhase.quantity = $("#phaseQuantity" + i + phases[j].id).val();
						orderMaterial.orderMaterialPhases.push(orderMaterialPhase);
					}
				order.orderMaterials.push(orderMaterial);
			}
			
			$.ajax({
				url: "Order.ashx?",
				type: "POST",
				dataType: "script",
				data: {
					numberOfMaterial: numberOfMaterial,
					action: $("#action").val(),
					payMethodType: $("[name=payMethodType]:checked").val(),
					payMethodAmount: $("#amount" + $("[name=payMethodType]:checked").val()).val(),
					order: JSON.stringify(order)
				}
			});
		}
		function AddMaterial() {
			var newTr;
			if (numberOfMaterial++ == 0)
			{
				$('#tblOrder tbody').append("<tr>");
				newTr = $('#tblOrder tbody>tr:last');
			}
			else newTr = $("<tr" + (numberOfMaterial % 2 == 0 ? " style='background-color:SeaShell'" : "") + "></tr>").insertAfter('#tblOrder tbody>tr:last');
			$(newTr).append("<td><%= selectMaterial%></td>".replace("comboMaterials", "comboMaterials" + numberOfMaterial));
			$(newTr).append("<td><input type=\"text\" id=\"description" + numberOfMaterial + "\" style=\"width:100%\" /></td>");
			$("<%=divSelectPhases.Replace("\"", "\\\"") %>".replace(/divSelectPhase/g, "divSelectPhase" + numberOfMaterial).replace(/materialIndex/g, numberOfMaterial.toString())).insertAfter('#customerId');
			$("#divSelectPhase" + numberOfMaterial).dialog({
				autoOpen: false,
				modal: true,
				width: 'auto',
				height: 'auto',
				maxHeight: 1300,
				title: 'Chọn công đoạn',
				resizable: false
			});
			$(newTr).append("<td><a href=\"javascript:;\" onclick=\"$('#divSelectPhase" + numberOfMaterial + "').dialog('open')\" >Chọn công đoạn</a></td>");
			$(newTr).append("<td id=\"price" + numberOfMaterial + "\">0</td>");
			$(newTr).append("<td><input type=\"text\" id=\"quantity" + numberOfMaterial + "\" value=\"0\" size=\"2\" onChange=\"RefreshMoney(numberOfMaterial)\"/></td>");
			$(newTr).append("<td id=\"money" + numberOfMaterial + "\">0</td>");
			$("#quantity" + numberOfMaterial).numeric({emptyValue: false, minValue: 1, increment: 1});
			for (var i in phases)
				$("#phaseQuantity" + numberOfMaterial + phases[i].id).numeric({emptyValue: 0, minValue: 0, increment: 1});
		}
		function RefreshPrice(materialIndex) {
			var currentPrice = 0;
			for (var i in phases)
				if ($("#phaseQuantity" + materialIndex + phases[i].id).val() != "")
				{
					if ($("#phaseWeight" + materialIndex + phases[i].id).val() != "" && !isNaN(parseFloat($("#phaseWeight" + materialIndex + phases[i].id).val())))
						currentPrice += Math.round(phases[i].price * parseFloat($("#phaseWeight" + materialIndex + phases[i].id).val()) * $("#phaseQuantity" + materialIndex + phases[i].id).val());
					else
						currentPrice += phases[i].price * $("#phaseQuantity" + materialIndex + phases[i].id).val();
				}
			$("#price" + materialIndex).html(currentPrice);
			RefreshMoney(materialIndex);
		}
		function RefreshMoney(materialIndex){
			$("#money" + materialIndex).text(Math.round($("#price" + materialIndex).text() * $("#quantity" + materialIndex).val()));
			totalPrice = 0;
			for (var i=1; i <= numberOfMaterial; i++)
				totalPrice += parseInt($("#money" + i).text());
			if (!isNaN(parseInt($("#discount").val())))
				totalPrice -= parseInt($("#discount").val());
			$("#totalPrice").text("Tổng cộng: " + totalPrice);
		}
	</script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
	<div style="background-image: '../styles/images/center.jpg'">
		<h2>CHI TIẾT ĐƠN HÀNG</h2>
	</div>
	<div class="Dan">
		<div class="div-row">
			Tên khách hàng: <span id="customerName" style="font-size: large"><%=currentCustomer.name %></span></div>
		<div class="div-row">
			Địa chỉ: <span id="address"><%=currentCustomer.address %></span>
		</div>
		<div class="div-row">
			Địa chỉ liên hệ: <span id="contactAddress"><%=currentCustomer.contactAddress %></span>
		</div>
		<div class="div-row">
			Người liên hệ: <span id="contactPerson"><%=currentCustomer.contactPerson %></span>
		</div>
		<div class="div-row">
			<label class="div-cell">
				Số đt: <span id="phone"><%=currentCustomer.phone %></span>
			</label>
			<label class="div-cell">
				Mobile: <span id="mobile"><%=currentCustomer.mobile %></span>
			</label>
			<label class="div-cell">
				Fax: <span id="fax"><%=currentCustomer.fax %></span>
			</label>
		</div>
		<div class="div-row">
			<label class="div-cell">
				Số tài khoản: <span id="accountNumber"><%=currentCustomer.accountNumber %></span>
			</label>
			<label class="div-cell">
				Ngân hàng: <span id="bank"><%=currentCustomer.bank %></span>
			</label>
		</div>
		<div class="div-row">
			<label class="div-cell">
				Mã số thuế: <span id="taxNumber"><%=currentCustomer.taxNumber %></span>
			</label>
			<label class="div-cell" style="width:auto">
				Giám đốc: <span id="director"><%=currentCustomer.director %></span>
			</label>
		</div>
	</div>
	<p />
	<table id="tblOrder" cellpadding="7px" style="width: 100%; border-top-color: firebrick;
		border-right-color: firebrick; border-bottom-color: firebrick; border-left-color: firebrick;
		border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px;
		border-top-style: ridge; border-right-style: ridge; border-bottom-style: ridge;
		border-left-style: ridge; border-collapse: collapse; background-color: wheat;">
		<thead>
			<tr style="color: azure; font-weight: bold; white-space: nowrap; background-color: black;">
				<th scope="col">
					Nguyên liệu <a href="javascript:AddMaterial();">Thêm</a>
				</th>
				<th scope="col">
					Quy cách
				</th>
				<th scope="col">
					Công đoạn
				</th>
				<th scope="col">
					Giá
				</th>
				<th scope="col" style="width: 90px">
					Số lượng
				</th>
				<th scope="col">
					Thành tiền
				</th>
			</tr>
		</thead>
		<tbody>
            
		</tbody>
	</table>
	<p />
	<div id="totalPrice" style="font-size: 14pt; font-weight: bold; float: right">Tổng cộng: <%=currentOrder.totalPrice %></div>
	<div>
		<div style="padding-bottom: 6px">Tổng tiền bằng chữ: <input type="text" id="strTotalPrice" size="80" value="<%=currentOrder.strTotalPrice %>"/></div>
		<div style="padding-bottom: 6px">Giảm giá: <input type="text" id="discount" size="15" onchange="RefreshMoney(1)" value="<%=currentOrder.discount %>"/>
		&nbsp;&nbsp; Số tiền đưa trước: <input type="text" id="prePay" size="15" value="<%=currentOrder.prePay %>"/>
		&nbsp;&nbsp; Số tiền còn lại: <input type="text" id="remain" size="15" value="<%=currentOrder.remain %>"/></div>
		<div style="padding-bottom: 6px">Hình thức thanh toán:
			<label><input type="radio" name="payMethodType" value="1" checked="checked" />Khi giao hóa đơn &nbsp;&nbsp;</label>
			<label><input type="radio" name="payMethodType" value="2" />Sau <input type="text" id="amount2" size="1" /> ngày &nbsp;&nbsp;</label>
			<label><input type="radio" name="payMethodType" value="3" />Thu trước khi giao <input type="text" id="amount3" size="1" />%</label></div>
		<div style="padding-bottom: 6px">Đóng gói:
		<label>
			<input type="radio" name="packageId" value="1" checked="checked" />Kiện sắt &nbsp;&nbsp;</label><label><input
				type="radio" name="packageId" value="2" />Kiện gỗ &nbsp;&nbsp;</label><label><input
					type="radio" name="packageId" value="3" />Giao lẻ &nbsp;&nbsp;</label></div>
		<div style="padding-bottom: 6px">Ngày giao hàng:	<input type="text" id="deliveryDate" value="<%=currentOrder.deliveryDate %>"/></div>
		<div style="padding-bottom: 6px">Tên nhân viên: <%= Session["Username"].ToString()%></div>
	</div>
	<%--<button id="btnSave" onclick="SubmitOrder(); return false;" style="text-align: center" >&nbsp;Lưu&nbsp;</button>--%>
	<button onclick="SubmitOrder(); return false;" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary icon-save"><img src="../styles/images/save.png" alt="Lưu" style="border:0; width:20px; padding: 3px 0px 3px 7px" /><span class="ui-button-text" style="float: right; padding-left: 0.8em">Lưu</span></button>
	<input type="hidden" value="create" id="action" value="edit"/>
	<input type="hidden" value="-1" id="customerId" value="<%=currentOrder.customerId %>"/>
</asp:Content>